﻿using Learun.Application.TwoDevelopment.ZZDT_EC;
using Learun.Util;
using NPOI.Util.Collections;
using Pipelines.Sockets.Unofficial.Arenas;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Runtime;
using System.Web;
using System.Web.Http;

namespace Learun.Application.Web.AppApi
{
    /// <summary>
    /// 绘制布置图相关的（By YuXH）
    /// </summary>
    [HandlerApiLogin(FilterMode.Ignore)]
    public class PlotLayoutApiController : WebApiControllerBase
    {

        /// <summary>
        /// 拿项目的所属系统。所属系统是一个特定的数据字典。至于哪个作为所属系统，则从settingAPI里查询
        /// </summary>
        /// <param name="ProjectId"></param>
        /// <returns>返回TreeModel</returns>
        [HttpGet]
        public IHttpActionResult GetSystemList(string ProjectId)
        {
            var ec_propertyBLL = new ec_propertyBLL();
            var ec_dataitemBLL = new ec_dataitemBLL();
            var settings = new ec_projectSettingsBLL();
            List<TreeModel> treeList = new List<TreeModel>();
            var FrameListFlg = settings.GetEntity("所属系统", ProjectId);

            if (FrameListFlg != null)
            {
                //从setting找属性
                var property = ec_propertyBLL.GetList("{PropertyName:\"" + FrameListFlg.SettingValue + "\",ProjectId:\"" + ProjectId + "\"}").FirstOrDefault();
                if (property != null)
                {
                    //从属性去找数据字典
                    var res = ec_dataitemBLL.GetDetailList(property.EnumData, "", ProjectId, false);
                    foreach (var item in res)
                    {
                        TreeModel node = new TreeModel();
                        node.id = item.DataItemDetailID;
                        node.text = item.DataItemName + "," + item.DataItemCode; //名字 + 编码 20230512;
                        node.value = item.DataItemCode;
                        node.showcheck = false;
                        node.checkstate = 0;
                        node.isexpand = true;
                        node.parentId = item.UpDataItemDetailID == null ? "0" : item.UpDataItemDetailID;
                        treeList.Add(node);


                    }
                    treeList = treeList.ToTree();
                    return Success(treeList);
                }
                else
                {
                    return Fail($"属性中（property表），没有找到【{FrameListFlg.SettingValue}】的属性。");
                }



            }
            else
            {
                return Fail("项目设置中（projectSettings表），没有找到【所属系统】的设置项。");
            }

        }
        /// <summary>
        /// 根据选中的所属系统，返回位号id，fileid和md5
        /// </summary>
        /// <param name="ProjectId"></param>
        /// <param name="DataItemNames">数据字典里的枚举值，以逗号分开，不需要带引号</param>
        /// <returns>返回List<treeModel>,id是对象id，text是fileId，value是MD5</treeModel></returns>
        [HttpGet]
        public IHttpActionResult GetLibraryFileBySelectedSystem(string ProjectId, string DataItemNames)
        {
           var res = new ec_library_fileService().GetFileOnSystemDwgByTagId(ProjectId, DataItemNames);
            return Success(res);
        }

        /// <summary>
        /// 拿项目的肋位号。肋位号是一个特定的数据字典。至于哪个作为肋位号，则从settingAPI里查询
        /// </summary>
        /// <param name="ProjectId"></param>
        /// <returns></returns>
        [HttpGet]
        public IHttpActionResult GetFrameList(string ProjectId)
        {
            var ec_dataitemBLL = new ec_dataitemBLL();
            var settings = new ec_projectSettingsBLL();

            var FrameListFlg = settings.GetEntity("肋位号", ProjectId);

            if (FrameListFlg != null)
            {
                List<FrameList> frameLists = new List<FrameList>();


                var res = ec_dataitemBLL.GetDetailList(FrameListFlg.SettingValue, "", ProjectId, false);
                foreach (var item in res)
                {
                    double value;
                    if (double.TryParse(item.DataItemCode, out value))
                    {
                        frameLists.Add(new FrameList() { Num = item.DataItemName, Value = value });
                    }
                }

                frameLists = frameLists.OrderBy(t => t.Value).ToList();
                return Success(frameLists);
            }
            else
            {
                return Fail("项目设置中（projectSettings表），没有找到【肋位号】的设置项。");
            }

        }

        /// <summary>
        /// 根据ObjectTypeID，查绘制布置图时的图例
        /// </summary>
        /// <param name="ProjectId"></param>
        /// <param name="objectTypeID"></param>
        /// <returns>返回一个LibraryFileID属性的json对象</returns>
        [HttpGet]
        public IHttpActionResult GetLibraryFileByType(string ProjectId, string objectTypeID)
        {
            //DefaultLayoutLibFileID

            var res = new ec_objecttypeBLL().GetEntity(objectTypeID, ProjectId);
            if (res == null)
            {
                return Fail("无效的ObjectTypeID");
            }
            if (string.IsNullOrEmpty(res.DefaultLayoutLibFileID))
            {
                return Success("0");
            }
            else
            {
                var fileObj = new ec_library_fileBLL().GetEntity(res.DefaultLayoutLibFileID, ProjectId);
                if (fileObj != null)
                {
                    return Success(("{LibraryFileID:\"" + res.DefaultLayoutLibFileID + "\"}").ToJObject());
                }
                else
                {
                    return Success("0");
                }
            }

        }

        /// <summary>
        /// 查绘制布置图时的图例。
        /// </summary>
        /// <param name="ProjectId"></param>
        /// <returns>返回所有有DefaultLayoutLibFileID的对象类型集合</returns>
        [HttpGet]
        public IHttpActionResult GetLibraryFileAll(string ProjectId)
        {
            //获取当前项目的所有对象类型数据
            var allObjectData = new ec_objecttypeBLL().GetList("{ProjectId:\"" + ProjectId + "\"}").ToList();
            List<ec_objecttypeEntity> resAll = new List<ec_objecttypeEntity>();
            foreach (var res in allObjectData)
            {
                if (string.IsNullOrEmpty(res.DefaultLayoutLibFileID))
                {
                    continue;
                }
                else
                {
                    var fileObj = new ec_library_fileBLL().GetEntity(res.DefaultLayoutLibFileID, ProjectId);
                    if (fileObj != null)
                    {
                        resAll.Add(res);
                    }
                    else
                    {
                        continue;
                    }
                }
            }
            return Success(resAll);
        }

    }


    /// <summary>
    /// 肋位号
    /// </summary>
    public class FrameList
    {
        /// <summary>
        /// 号
        /// </summary>
        public string Num { get; set; }

        /// <summary>
        /// 值。单位是m
        /// </summary>
        public double Value { get; set; }
    }

}